package preloader
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import mx.events.FlexEvent;
	import mx.graphics.codec.PNGEncoder;
	import mx.preloaders.DownloadProgressBar;
	import mx.states.AddChild;
	
	public class Preloader extends DownloadProgressBar
	{
		//显示进度文字
		private var progressText:TextField;
		//进度条
		public var img:WelcomeScreen;
		//logo页面
		public var logo:WelcomeLogo;
		//private var _timer:Timer;
		
		public function Preloader()
		{
			super();
			
			//加入 logo
			logo = new WelcomeLogo();
			this.addChild(logo);
			//加入进度条
			img = new WelcomeScreen();
			//this.addChild(img);
			// 加入进度文字
			progressText = new TextField();
			progressText.x = 50;
			progressText.y = 100;
			progressText.width = 200;
			this.addChild(progressText);

			//进度条计时器初始化，我们事先进度条的原理就是不停的刷新进度条图片
			//因为每次进度条的长度不同，所以就有进度条在走的效果
			//_timer = new Timer(20);
			//_timer.addEventListener(TimerEvent.TIMER, drawTimerHandler);
			//_timer.start();
			
			x = (stageWidth/2) - (300/2);
			y = (stageHeight/2) - (180/2);
		}
		
		//override这个函数，来实现自己Preloader的设置，而不是用其默认的设置
		override public function set preloader(value:Sprite):void{
			
			value.addEventListener(ProgressEvent.PROGRESS, progHandler);
			value.addEventListener(FlexEvent.INIT_COMPLETE, overrideInitCompleteHandler);
			//在这里设置预载界面居中，如果在初始化函数中设置，会有stageWidth和最终界面大小不一致的错误，
			//而导致不能居中
			//x = (stageWidth/2) - (300/2);
			//y = (stageHeight/2) - (180/2);
			
		}
		
		private var prog:Number;
		private function progHandler(e:ProgressEvent):void{
			
			//计算进度，并且设置文字进度和进度条的进度
			prog = e.bytesLoaded / e.bytesTotal * 100;
			progressText.text = "已下载 " + String(int(prog)) + "% 请耐心等待……";
			
			//if (img){
			//	img.progress = prog;
			//}
			var s:Sprite = new Sprite();
			var g:Graphics = s.graphics;
			g.clear();  
			g.beginFill(0x88ff22);  
			g.drawRect(50,130, 100*2, 20);  
			g.endFill();  
			g.beginFill(0xff9900);  
			g.drawRect(50,130, prog*2, 20);  
			g.endFill();
			
			this.addChild(s);

		}
		
		private function compHandler(e:Event):void{
		
		}
		
		private function overrideInitCompleteHandler(e:FlexEvent):void{
			
			//如果载入完毕，则停止刷新
			img.ready = true;
			//_timer.stop();
			
			//测试专用，下载完毕后，不马上跳到程序的默认界面。而是停顿10秒后再跳入。
			//var timer:Timer = new Timer(5000, 1);
			//timer.addEventListener(TimerEvent.TIMER, dispatchComplete);
			//timer.start();
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private function initProgHandler(e:FlexEvent):void{
		
		}
		
		//一定要分发这个事件，来通知程序已经完全下载，可以进入程序的默认界面了
		private function dispatchComplete(event:TimerEvent):void{
			
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
		
		//每个时钟周期都刷新进度条图片

		//private function drawTimerHandler(event:TimerEvent):void{
			//img.refresh();
		//}
		
	}
	
	
}